<!--
 * @Author: 471826078@qq.com
 * @Date: 2020-06-03 11:25:02
 * @LastEditors: younghxp
 * @LastEditTime: 2022-02-21 11:21:59
-->
<template>
  <div class="perfectCount">
    <div class="header">
      <span>统计的是系统内待完善的项目，只统计状态为【已开工】的项目</span>
      <div class="headerBtn">
        <!-- <el-button type="primary" class="funComBut saveBtn">列设置</el-button> -->
        <div class="colSetupBottom" style="float: left">
          <!-- <colSetup :datas="colSetupTableCol" @getNewList='getNewList'></colSetup> -->
          <colSetup
            :tableColList="colSetupTableCol"
            @getSelectMenuId="getSelectMenuIds"
            :mark="mark"
          />
        </div>
        <el-button
          type="primary"
          class="funComBut saveBtn"
          icon="icon iconfont icondaochu"
          @click="toExportExcel"
          style="margin-left: 12px"
        >
          导出报表
        </el-button>
      </div>
    </div>
    <el-table
      :data="tableList"
      :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
      v-loading="loading"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      row-key="id"
      @filter-change="handleFilterChange"
      border
      lazy
      :load="load"
      style="border: 1px solid #dfe6ec"
      :max-height="tableHeight"
      ref="tableBox1"
      @header-dragend="handleDragend"
    >
      <el-table-column
        :prop="item.prop"
        :label="item.label"
        align="left"
        :formatter="formatTd"
        v-for="(item, index) in fixedList"
        :key="item.id"
        :fixed="item.fixed"
        :sortable="
          ['industryName', 'investmentName', 'areaName'].includes(item.prop)
            ? false
            : true
        "
        :filterField="
          item.prop === 'industryName'
            ? industryNameList
            : false || item.prop === 'investmentName'
            ? investmentNameList
            : false
        "
        :column-key="item.prop"
        :filter-multiple="false"
        default-expand-all
        :min-width="
          item.prop === 'industryName' || item.prop === 'investmentName'
            ? 120
            : 320
        "
        v-if="item.isShow"
      >
        <template slot-scope="scope">
          <el-link
            v-if="index > 3"
            style="color: #409eff"
            @click="lookPerfevtDetail(scope, index)"
            >{{ scope.row[item.prop] }}</el-link
          >
          <span v-else>{{ scope.row[item.prop] }}</span>
        </template>
      </el-table-column>
      <!-- <my-column v-for="(item,index) in filterList" :key="index" :col="item"></my-column> -->
    </el-table>

    <exportExcel
      v-if="showExcelDialog"
      :showExcel="showExcel"
      :rowDimensionList="exportTableCol"
      :rowDimensionSelcted="rowDimensionSelcted"
      :colmnList="colSetupTableTotle"
      @closeExcel="closeExcel"
      @getExportList="getExportList"
    ></exportExcel>
  </div>
</template>
<script>
import exportExcel from '@/components/publicCom/exportExcel1'
// import colSetup from "@/components/publicCom/colSetupMore";
import MyColumn from '@/components/constructAttendance/MyColumn'

export default {
  name: 'perfectCount',
  components: {
    exportExcel,
    // colSetup,
    MyColumn,
  },
  props: {
    list: {
      type: Array,
    },
    activeName: {
      type: Number,
    },
  },
  data() {
    return {
      rowDimensionSelcted: 'area',
      exportTableCol: [
        {
          type: 'area',
          name: '只导出按地区维度统计',
        },
        {
          type: 'areaIndustry',
          name: '同时导出按地区和行业维度统计',
        },
        {
          type: 'areaIndustryInvestment',
          name: '同时导出地区、行业和投资类型维度统计',
        },
      ],
      exportIndustrySupervisor: [
        {
          type: 'area',
          name: '只导出按地区维度统计(管辖行业)',
        },
        {
          type: 'areaIndustryInvestment',
          name: '同时导出按地区维度(管辖行业)、投资类别维度统计',
        },
      ],
      investmentNameList: [
        { value: 'GOV', text: '政府投资' },
        { value: 'GOV_AND_SOCIETY', text: '政府与社会共筹' },
        { value: 'NATIONAL', text: '国企项目' },
        { value: 'NOT_GOV', text: '非政府投资' },
        { value: 'OTHERS', text: '其他工程项目' },
      ],
      tableHeight: null,
      showExcel: false,
      showExcelDialog: false,
      lists: [],
      tableList: [],
      requestUrl: 'api-s/cs/statsprojectimprove/filter',
      filterField: {
        industryId: 'total',
        investment: 'total',
      },
      loading: false,
      ExportList: [],
      colSetupTableCol: [],
      colSetupTableTotle: [],
      filterList: [],
      isShow: false,
      tablefilterField: {
        areaId: '',
        industryId: 'total',
        investment: 'total',
      },
      mark: 'projectDetailReport',
    }
  },
  created() {
    this.getTableList()
    this.getHeaderData()
  },
  computed: {
    fixedList: function () {
      this.filterList.forEach((v) => {
        if (
          v.prop === 'areaName' ||
          v.prop === 'industryName' ||
          v.prop === 'investmentName'
        ) {
          v.fixed = true
        }
      })
      return this.filterList.filter((item) => {
        return item.isShow
      })
    },
    industryNameList: function () {
      const vm = this
      let users = vm.$store.state.industryOptions
      let arr = []
      for (let item of users) {
        let obj = {
          value: '',
          text: '',
        }
        obj.value = item.code
        obj.text = item.name
        arr.push(obj)
      }
      return arr
    },
  },
  methods: {
    handleFilterChange(value) {
      if (value.industryName) {
        if (value.industryName.toString()) {
          this.tablefilterField.industryId = value.industryName.toString()
        } else {
          this.tablefilterField.industryId = 'total'
        }
      } else if (value.investmentName) {
        if (value.investmentName.toString()) {
          this.tablefilterField.investment = value.investmentName.toString()
        } else {
          this.tablefilterField.investment = 'total'
        }
      }
      this.getTableDate()
    },
    getTableDate() {
      this.axios({
        method: 'post',
        url: 'api-s/cs/statsprojectimprove/filter',
        data: this.tablefilterField,
      })
        .then((response) => {
          this.tableList = response.data.result
          this.$nextTick(() => {
            this.$refs.tableBox1.doLayout()
          })
        })
        .catch((error) => {})
    },
    /**
     * @description: 判断是否是行业主管
     */
    ishangyezhuguang() {
      if (this.$store.state.userType == 'DIRECTOR') {
        this.rowDimensionSelcted = 'area'
        this.exportTableCol = this.exportIndustrySupervisor
      }
    },
    toExportExcel() {
      this.showExcel = true
      this.showExcelDialog = true
    },
    closeExcel(val) {
      this.showExcel = false
      this.showExcelDialog = false
    },
    getTableList() {
      this.axios({
        method: 'post',
        url: 'api-s/cs/statsprojectimprove/filter',
        data: {
          industryId: 'total',
          investment: 'total',
        },
      })
        .then((response) => {
          this.tableList = response.data.result
          this.loading = false
          if (this.tableList.length < 1) {
            this.isShow = true
          }
          this.$nextTick(() => {
            this.$refs.tableBox1.doLayout()
          })
        })
        .catch((error) => {})
    },
    // load (tree, treeNode, resolve) {
    //   const vm = this;
    //   this.axios({
    //     method: "post",
    //     url: "/api-s/cs/statsprojectimprove/findImproveSListByDetail",
    //     data: {
    //       id: tree.id,
    //       areaId: tree.areaId,
    //       isStats: tree.isStats,
    //       industryId: tree.industryId || " ",
    //     },
    //   }).then((res) => {
    //     let tableArray = res.data.result;
    //     tableArray.forEach((item, index) => {
    //       delete item.areaName;
    //     });
    //     resolve(res.data.result);
    //     if (res.data.result.length == 0) {
    //       this.$message({
    //         type: "error",
    //         message: "本节点以下暂无子节点!",
    //       });
    //     }
    //   });
    // },
    /**
     * @description: 懒加载表格的子节点数据
     */
    load(tree, treeNode, resolve) {
      const vm = this
      let industryValue
      if (vm.$store.state.userType == 'DIRECTOR') {
        industryValue = vm.$store.state.industryId
      } else {
        industryValue = tree.industryId || ' '
      }
      let industryId
      if (tree.industryId) {
        industryId = tree.industryId
      } else {
        industryId = 'all'
      }
      let investment
      if (tree.industryId) {
        investment = 'all'
      } else if (this.tablefilterField.investment) {
        investment = this.tablefilterField.investment
      } else {
        industryId = 'total'
      }
      this.axios({
        method: 'post',
        url: 'api-s/cs/statsprojectimprove/filter',
        data: {
          id: tree.id,
          areaId: tree.areaId,
          isStats: tree.isStats,
          industry: industryValue,
          industryId: industryId,
          investment: investment,
        },
      }).then((res) => {
        let tableArray = res.data.result
        tableArray.forEach((item, index) => {
          delete item.areaName
        })
        resolve(res.data.result)
        if (res.data.result.length == 0) {
          this.$message({
            type: 'error',
            message: '本节点以下暂无子节点!',
          })
        }
      })
    },
    getExportList(val) {
      this.ExportList = val[this.activeName].children
      this.showExcel = false
      this.$alert('报表生成中，请稍等', '温馨提示', {
        confirmButtonText: '确定',
        callback: (action) => {},
      })
    },
    /**
     * @name: 查看明细
     * @param {item}
     * @Author: 471826078@qq.com
     */
    lookPerfevtDetail(item, index) {
      const { column, row } = item
      let type = ''
      if (column.property == 'realTotal') {
        type = '2'
      } else if (column.property == 'realAttTotal') {
        type = '3'
      } else if (column.property == 'inputBankTotal') {
        type = '4'
      } else if (column.property == 'depositFundTotal') {
        type = '5'
      } else if (column.property == 'payTotal') {
        type = '6'
      } else if (column.property == 'lastNonthPayTotal') {
        type = '7'
      } else if (column.property == 'payDayTotal') {
        type = '8'
      } else if (column.property == 'psTotal') {
        type = '9'
      } else if (column.property == 'depositAccassTotal') {
        type = '10'
      } else if (column.property == 'availableFiscalTotal') {
        type = '11'
      } else if (column.property == 'noEquipAttMachineTotal') {
        type = '12'
      }

      const areaId = row.areaId ? row.areaId : ''
      const industryId = row.industryId ? row.industryId : ''
      const industry =
        this.tablefilterField.industryId != 'total'
          ? this.tablefilterField.industryId
          : ''
      const investmentType =
        this.tablefilterField.investment != 'total'
          ? this.tablefilterField.investment
          : ''
      const status = 'STARTED'
      const investment = row.investment ? row.investment : ''
      this.$emit('callBackActive', {
        areaId,
        industryId,
        status,
        investment,
        industry,
        investmentType,
        type,
      })
    },
    // 获取表头数据
    getHeaderData() {
      const vm = this
      vm.axios({
        method: 'post',
        url: '/api-s/dimension/head',
        data: { type: 'improve_stats', complex: '1' },
      })
        .then((response) => {
          var arr1 = []
          arr1 = response.data.result[0].children
          vm.filterList = arr1
          // vm.getSelectProps(vm.filterList);
          var arr2 = [response.data.result[0]]
          var exportArr = response.data.result
          for (let i = 0, len = arr2.length; i < len; i++) {
            for (
              let j = 0, childLen = arr2[i].children.length;
              j < childLen;
              j++
            ) {
              var childrenData = arr2[i].children[j]
              if (
                childrenData.prop == 'areaName' ||
                childrenData.prop == 'industryName' ||
                childrenData.prop == 'investmentName'
              ) {
                childrenData.disabled = true
              }
            }
          }
          if (!this.isEsUser()) {
            vm.filterList.pop()
            // var childData = arr2[0].children;
            // childData.pop()
            exportArr.pop()
          }
          vm.colSetupTableCol = JSON.parse(JSON.stringify(arr2))
          vm.colSetupTableTotle = JSON.parse(JSON.stringify(exportArr))
        })
        .catch((error) => {
          vm.$message({
            showClose: true,
            duration: 3000,
            message: '获取全部项目表头失败',
            type: 'error',
          })
        })
    },
    getSelectProps(tableCol) {
      let propArr = []
      tableCol.forEach((v) => {
        if (v.isShow) {
          propArr.push(v.prop)
          v.children.forEach((item) => {
            if (item.isShow) {
              propArr.push(item.prop)
            }
          })
        }
      })
      this.selectProps = JSON.parse(JSON.stringify(propArr))
    },
    getSelectMenuIds(val, newVal) {
      this.filterList = val
      this.$nextTick(() => {
        this.$refs.tableBox1.doLayout()
      })
    },
  },
  mounted() {
    this.ishangyezhuguang()
    this.$nextTick(function () {
      this.tableHeight = window.innerHeight - 180
      // 监听窗口大小变化
      let self = this
      window.onresize = function () {
        self.tableHeight = window.innerHeight - 180
      }
    })
  },
}
</script>
<style lang="less" scoped>
.perfectCount {
  padding: 24px;
}
.header {
  position: relative;
  padding-bottom: 16px;
  line-height: 36px;
  box-sizing: border-box;
  span {
    font-size: 14px;
    color: #333333;
  }
  &Btn {
    position: absolute;
    right: 0;
    top: 0;
    height: 36px;
  }
}
</style>
